﻿
Namespace myBookmarks.Web
    Public Class Pager
        Private viewContext As ViewContext
        Private ReadOnly pageSize As Integer
        Private ReadOnly currentPage As Integer
        Private ReadOnly totalItemCount As Integer
        Private ReadOnly linkWithoutPageValuesDictionary As RouteValueDictionary

        Public Sub New(ByVal viewContext As ViewContext, ByVal pageSize As Integer, ByVal currentPage As Integer, ByVal totalItemCount As Integer, ByVal valuesDictionary As RouteValueDictionary)
            Me.viewContext = viewContext
            Me.pageSize = pageSize
            Me.currentPage = currentPage
            Me.totalItemCount = totalItemCount
            Me.linkWithoutPageValuesDictionary = valuesDictionary
        End Sub

        Public Function RenderHtml() As String
            Dim pageCount As Integer = CInt(Math.Ceiling(Me.totalItemCount / CDbl(Me.pageSize)))
            If pageCount > 1 Then


                Dim nrOfPagesToDisplay As Integer = 10

                Dim sb = New StringBuilder()

                ' Previous
                If Me.currentPage > 1 Then
                    sb.Append(GeneratePageLink("&lt;", Me.currentPage - 1))
                Else
                    sb.Append("<span class=""disabled"">&lt;</span>")
                End If

                Dim start As Integer = 1
                Dim [end] As Integer = pageCount

                If pageCount > nrOfPagesToDisplay Then
                    Dim middle As Integer = CInt(Math.Ceiling(nrOfPagesToDisplay / 2.0R)) - 1
                    Dim below As Integer = (Me.currentPage - middle)
                    Dim above As Integer = (Me.currentPage + middle)

                    If below < 4 Then
                        above = nrOfPagesToDisplay
                        below = 1
                    ElseIf above > (pageCount - 4) Then
                        above = pageCount
                        below = (pageCount - nrOfPagesToDisplay)
                    End If

                    start = below
                    [end] = above
                End If

                If start > 3 Then
                    sb.Append(GeneratePageLink("1", 1))
                    sb.Append(GeneratePageLink("2", 2))
                    sb.Append("...")
                End If
                For i As Integer = start To [end]
                    If i = Me.currentPage Then
                        sb.AppendFormat("<span class=""current"">{0}</span>", i)
                    Else
                        sb.Append(GeneratePageLink(i.ToString(), i))
                    End If
                Next
                If [end] < (pageCount - 3) Then
                    sb.Append("...")
                    sb.Append(GeneratePageLink((pageCount - 1).ToString(), pageCount - 1))
                    sb.Append(GeneratePageLink(pageCount.ToString(), pageCount))
                End If

                ' Next
                If Me.currentPage < pageCount Then
                    sb.Append(GeneratePageLink("&gt;", (Me.currentPage + 1)))
                Else
                    sb.Append("<span class=""disabled"">&gt;</span>")
                End If
                Return sb.ToString()
            End If
            Return String.Empty
        End Function

        Private Function GeneratePageLink(ByVal linkText As String, ByVal pageNumber As Integer) As String
            Dim pageLinkValueDictionary = New RouteValueDictionary(Me.linkWithoutPageValuesDictionary)
            pageLinkValueDictionary.Add("page", pageNumber)
            'var virtualPathData = this.viewContext.RouteData.Route.GetVirtualPath(this.viewContext, pageLinkValueDictionary);
            Dim queryString As NameValueCollection = Me.viewContext.HttpContext.Request.QueryString
            For Each key As String In queryString
                If key IsNot Nothing Then
                    If Not pageLinkValueDictionary.ContainsKey(key) Then
                        If Not String.IsNullOrEmpty(queryString(key)) Then
                            pageLinkValueDictionary(key) = queryString(key)
                        End If
                    End If
                End If
            Next
            Dim virtualPathData = RouteTable.Routes.GetVirtualPath(Me.viewContext.RequestContext, "Default", pageLinkValueDictionary)
            If virtualPathData IsNot Nothing Then
                Dim linkFormat As String = "<a href=""{0}"">{1}</a>"
                Return [String].Format(linkFormat, virtualPathData.VirtualPath, linkText)
            Else
                Return Nothing
            End If
        End Function
    End Class
End Namespace
